package net.lainiao.morethread.Chapter1.demo9;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadFactory;

/**
 * Created by Administrator on 2017/2/28.
 */
public class MyTheadFactory implements ThreadFactory {
    private int counter;
    private String name;
    private List<String> stats = new ArrayList<>();

    public MyTheadFactory(String name){
        this.counter=0;
        this.name=name;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread=new Thread("MyTheadFactory-Thread-"+counter);
        counter++;
        stats.add(String.format("Thread %s Created with Name %s On %s", thread.getId(),thread.getName(),new Date()));
        return thread;
    }

    public String getStates(){
        StringBuffer buffer=new StringBuffer();
        for (String name :stats){
            buffer.append(name+"\n");
        }
        return buffer.toString();
    }

}
